draw_chart_battery_events_hourly_cluster = (rows_data) ->
  data = new google.visualization.DataTable()

  data.addColumn 'number', 'Hour'
  data.addColumn 'number', 'Power Connected'
  data.addColumn 'number', 'Power Disconnected'
  data.addRows rows_data
   
  options =
    title:'Battery Events Hourly Distribution'
    vAxis:
      title:'Quantity'
    hAxis:
      title:'Hours'
      viewWindow:
        min: 0
        max: 23
      gridlines:
        count: 24

  chart = new google.visualization.ColumnChart document.getElementById('battery_events_hourly_cluster_chart')
  chart.draw data, options

draw_chart_connectivity_events_hourly_cluster = (rows_data) ->
  data = new google.visualization.DataTable()

  data.addColumn 'number', 'Hour'
  data.addColumn 'number', '3G Connected'
  data.addColumn 'number', '3G Disconnected'
  data.addColumn 'number', 'Wi-Fi Connected'
  data.addColumn 'number', 'Wi-Fi Disconnected'
  data.addRows rows_data
   
  options =
    title:'Hour Distribution of Connectivity Events'
    vAxis:
      title:'Quantity'
    hAxis:
      title:'Hours'
      viewWindow:
        min: 0
        max: 23
      gridlines:
        count: 24

  chart = new google.visualization.ColumnChart document.getElementById('connectivity_events_hourly_cluster_chart')
  chart.draw data, options
  
draw_chart_charging_or_not_by_time = (rows_data) ->
  data = new google.visualization.DataTable()
  data.addColumn 'datetime', 'Time'
  data.addColumn 'number', 'Charging (1) or Not (0)'

  for row in rows_data
    row[0] = new Date Number(row[0]) * 1000
   
  data.addRows rows_data
   
  options =
    title:'Charging Time Series'
    vAxis:
      viewWindow:
        min: 0
        max: 1
      gridlines:
        count: 2
                    
  chart = new google.visualization.LineChart document.getElementById('charging_or_not_by_time_chart')
  chart.draw data, options
  
draw_chart_connected_or_not_by_time = (rows_data, chart_title, element_id) ->
  data = new google.visualization.DataTable()
  data.addColumn 'datetime', 'Time'
  data.addColumn 'number', 'Connected (1) or Not (0)'

  for row in rows_data
    row[0] = new Date Number(row[0]) * 1000
   
  data.addRows rows_data
   
  options =
    title: chart_title
    vAxis:
      viewWindow:
        min: 0
        max: 1
      gridlines:
        count: 2
                    
  chart = new google.visualization.LineChart document.getElementById(element_id)
  chart.draw data, options
    
draw_chart_not_charging_duration_by_time = (rows_data)->
  data = new google.visualization.DataTable()
  data.addColumn 'number', 'Time'
  data.addColumn 'number', 'Duration'
  data.addRows rows_data
   
  options = 
    title:'Not Charging Duration by Time'
    legend: 'none'
    trendlines: { 0: {} }
    vAxis:
      title: 'Duration in Minutes'
    hAxis:
      gridlines:
        count: -1
                    
  chart = new google.visualization.LineChart document.getElementById('not_charging_duration_by_time_chart')
  chart.draw data, options
  
draw_chart_no_connectivity_duration_by_time = (rows_data, chart_title, element_id)->
  data = new google.visualization.DataTable()
  data.addColumn 'number', 'Time'
  data.addColumn 'number', 'Duration'
  data.addRows rows_data
   
  options = 
    title: chart_title
    trendlines: { 0: {} }
    vAxis:
      title: 'Duration in Minutes'
    hAxis:
      gridlines:
        count: -1
                    
  chart = new google.visualization.LineChart document.getElementById(element_id)
  chart.draw data, options

$(document).ready ->
  drawAllCharts = ->
    url_params = 'user_uuid=' + window.user_uuid + '&days_ago=' + window.days_ago + '&threshold=' + window.threshold
    
    $.getJSON '/battery_events_hourly_cluster_chart_data?' + url_params, (json_data) ->
      draw_chart_battery_events_hourly_cluster json_data
    $.getJSON '/connectivity_events_hourly_cluster_chart_data?' + url_params, (json_data) ->
      draw_chart_connectivity_events_hourly_cluster json_data
    $.getJSON '/charging_or_not_by_time_chart_data?' + url_params, (json_data) ->
      draw_chart_charging_or_not_by_time json_data
    $.getJSON '/connected_or_not_by_time_chart_data?' + url_params + '&net_type=1', (json_data) ->
      draw_chart_connected_or_not_by_time json_data, 'Connectivity 3G Time Series', 'connected_3G_or_not_by_time_chart'
    $.getJSON '/connected_or_not_by_time_chart_data?' + url_params + '&net_type=0', (json_data) ->
      draw_chart_connected_or_not_by_time json_data, 'Connectivity Wi-Fi Time Series', 'connected_WiFi_or_not_by_time_chart'
    $.getJSON '/not_charging_duration_by_time_chart_data?' + url_params, (json_data) ->
      draw_chart_not_charging_duration_by_time json_data
    $.getJSON '/no_connectivity_duration_by_time_chart_data?' + url_params + '&net_type=1', (json_data) ->
      draw_chart_no_connectivity_duration_by_time json_data, 'No 3G Connectivity Duration by Time', 'no_3G_connectivity_duration_by_time_chart'
    $.getJSON '/no_connectivity_duration_by_time_chart_data?' + url_params + '&net_type=0', (json_data) ->
      draw_chart_no_connectivity_duration_by_time json_data, 'No Wi-Fi Connectivity Duration by Time', 'no_WiFi_connectivity_duration_by_time_chart'
      
  window.user_uuid = $.url().param 'user_uuid'
  window.threshold = $.url().param 'threshold'
  window.days_ago = $.url().param 'days_ago'
  google.load 'visualization', '1.0', {'packages':['corechart'], 'callback' : drawAllCharts} 